package com.yiwei.zhh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.zhh.R;
import com.yiwei.zhh.api.callback.LoanDetailContract;
import com.yiwei.zhh.api.requestInfo.LoanDetailRequest;
import com.yiwei.zhh.constant.Const;
import com.yiwei.zhh.constant.UserGlobal;
import com.yiwei.zhh.model.BankListModel;
import com.yiwei.zhh.model.LoanDetailModel;
import com.yiwei.zhh.util.ToastUtil;
import com.yiwei.zhh.view.CircularProgress;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 2017/9/20.
 */

public class LoanDetailV2Activity extends BaseActivity implements LoanDetailContract.View {
    @BindView(R.id.txt_huanqing_money)
    TextView txtHuanqingMoney;
    @BindView(R.id.txt_huanqing_content)
    TextView txtHuanqingContent;
    @BindView(R.id.txt_huanqing_fee)
    TextView txtHuanqingFee;
    @BindView(R.id.txt_huanqing_loan_date)
    TextView txtHuanqingLoanDate;
    @BindView(R.id.btn_huanqing_check)
    TextView btnHuanqingCheck;
    @BindView(R.id.txt_huanqing_benjin)
    TextView txtHuanqingBenjin;
    @BindView(R.id.txt_huanqing_fuwu_fee)
    TextView txtHuanqingFuwuFee;
    @BindView(R.id.txt_huanqing_yuqi_fee)
    TextView txtHuanqingYuqiFee;
    @BindView(R.id.txt_huanqing_date)
    TextView txtHuanqingDate;
    @BindView(R.id.txt_shenhe_money)
    TextView txtShenheMoney;
    @BindView(R.id.txt_shenhe_content)
    TextView txtShenheContent;
    @BindView(R.id.btn_shenhe_check)
    TextView btnShenheCheck;
    @BindView(R.id.txt_fail_money)
    TextView txtFailMoney;
    @BindView(R.id.txt_fail_content)
    TextView txtFailContent;
    @BindView(R.id.btn_fail_check)
    TextView btnFailCheck;
    @BindView(R.id.txt_fail)
    TextView txtFail;
    @BindView(R.id.txt_fail_reason)
    TextView txtFailReason;
    @BindView(R.id.txt_wait_money)
    TextView txtWaitMoney;
    @BindView(R.id.txt_wait_content)
    TextView txtWaitContent;
    @BindView(R.id.txt_wait_fee)
    TextView txtWaitFee;
    @BindView(R.id.txt_wait_loan_date)
    TextView txtWaitLoanDate;
    @BindView(R.id.btn_wait_check)
    TextView btnWaitCheck;
    //    @BindView(R.id.txt_wait_fuwu_fee)
//    TextView txtWaitFuwuFee;
//    @BindView(R.id.txt_wait_date)
//    TextView txtWaitDate;

    @BindView(R.id.layout_shenhe)
    LinearLayout layoutShenhe;
    @BindView(R.id.layout_fail)
    LinearLayout layoutFail;
    @BindView(R.id.layout_daifangkuan)
    LinearLayout layoutDaifangkuan;
    @BindView(R.id.progressBar)
    CircularProgress progressBar;
    @BindView(R.id.txt_total_status)
    TextView txtTotalStatus;
    @BindView(R.id.txt_total_benjin)
    TextView txtTotalBenjin;
    @BindView(R.id.txt_total_fuwu_fee)
    TextView txtTotalFuwuFee;
    @BindView(R.id.layout_huanqing)
    LinearLayout layoutHuanqing;
    @BindView(R.id.txt_ing_money)
    TextView txtIngMoney;
    @BindView(R.id.txt_ing_status)
    TextView txtIngStatus;
    @BindView(R.id.txt_ing_content)
    TextView txtIngContent;
    @BindView(R.id.txt_ing_fee)
    TextView txtIngFee;
    @BindView(R.id.txt_ing_yuqi_fee)
    TextView txtIngYuqiFee;
    @BindView(R.id.txt_ing_loan_date)
    TextView txtIngLoanDate;
    @BindView(R.id.btn_ing_check)
    TextView btnIngCheck;
    @BindView(R.id.layout_ing)
    LinearLayout layoutIng;
    @BindView(R.id.txt_shenhe_loan_date)
    TextView txtShenheLoanDate;
    private Unbinder unbiner;
    private LoanDetailRequest api;
    private String id;
    private String dealId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentView(R.layout.activity_loan_detail_v2);
        unbiner = ButterKnife.bind(this);
        baseInitView(this, "LoanDetailV2Activity", Const.ViewType.ACTIVITY);
        initBar();
        init();
    }

    private void init() {
        setBackIntent();
        setTitleText("借款详情");
        api = LoanDetailRequest.create();
        api.attachView(this);
        id = getIntent().getStringExtra("id");
        api.getLoanDetail(id);
    }

    private void showLoading(boolean b) {
        if (progressBar != null) {
            if (b)
                progressBar.setVisibility(View.VISIBLE);
            else
                progressBar.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        api.detachView();
        api = null;
        unbiner.unbind();
    }

    @OnClick({R.id.btn_huanqing_check, R.id.btn_shenhe_check, R.id.btn_wait_check, R.id.btn_ing_check})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_ing_check:
            case R.id.btn_huanqing_check:

            case R.id.btn_shenhe_check:

            case R.id.btn_wait_check:
                Intent intent = new Intent(this, LoanAgreementActivity.class);
                intent.putExtra("type", 0);
                intent.putExtra("deal_id", dealId);
                UserGlobal.info.deal_id = dealId;
                UserGlobal.info.type = 0;
                startActivity(intent);
                overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                break;
        }
    }

    @Override
    public void showError() {

    }

    @Override
    public void complete() {
        showLoading(false);
    }

    @Override
    public void onRequestStart() {
        showLoading(true);
    }

    @Override
    public void getBankListSuccess(BankListModel item) {

    }

    @Override
    public void getLoanDetailSuccess(LoanDetailModel item) {
        if (item.getStatus() == 0) {
            ToastUtil.makeText(this, item.getMsg());
            return;
        }
        dealId = item.getData().getId() + "";
        LoanDetailModel.Data data = item.getData();
        switch (item.getStatus()) {
            case 1: // 审核中-未绑定银行卡
            case 2: // 待审核-已绑定银行卡
                layoutShenhe.setVisibility(View.VISIBLE);
                txtShenheContent.setText(data.sub_name);
                txtShenheLoanDate.setText(data.getTerm_of_loan());
                txtShenheMoney.setText(data.getMoney_format().replace("￥", ""));
                break;

            case 4: // 待放款-已绑定银行卡
            case 5:
                layoutDaifangkuan.setVisibility(View.VISIBLE);
                txtWaitContent.setText(data.sub_name);
//                txtWaitDate.setText(data.getTerm_of_loan());
                txtWaitLoanDate.setText(data.getRepay_day_format());
                txtWaitFee.setText(data.getServices_fee_format().replace("￥", ""));
//                txtWaitFuwuFee.setText(data.getTrue_services_money_format().replace("￥", ""));
                txtWaitMoney.setText(data.getMoney_format().replace("￥", ""));
                break;

            case 6: //未还款
                layoutIng.setVisibility(View.VISIBLE);
                txtIngMoney.setText(data.getNeed_repay_money() + "");
//                txtTotalStatus.setText("待还款");
                txtIngLoanDate.setText(data.getRepay_day_format());
                txtIngFee.setText(data.getServices_fee_format().replace("￥", ""));
//                txtHuanqingFuwuFee.setText(data.getTrue_services_money_format().replace("￥", ""));
                txtIngYuqiFee.setText(data.getImpose_money_format().replace("￥", ""));
                txtIngContent.setText(data.sub_name);
                if (data.getImpose_days() > 0) {

                    txtIngStatus.setText(data.getRepay_day_format() + "前还款,已逾期" + data.getImpose_days() + "天");
                } else {
                    txtIngStatus.setText(data.getRepay_day_format() + "前还款,剩" + data.getRemaining_days() + "天");
                }
                break;
            case 7: //已还清
                layoutHuanqing.setVisibility(View.VISIBLE);
                txtHuanqingMoney.setText(data.getMoney_format().replace("￥", ""));
                txtHuanqingBenjin.setText(data.getTrue_self_money_format().replace("￥", ""));
                txtHuanqingDate.setText(data.getTerm_of_loan());
                txtHuanqingLoanDate.setText(data.getTrue_repay_date());
                txtHuanqingFee.setText(data.getServices_fee_format().replace("￥", ""));
                txtHuanqingFuwuFee.setText(data.getTrue_services_money_format().replace("￥", ""));
                txtHuanqingYuqiFee.setText(data.getTrue_impose_money_format().replace("￥", ""));
                txtHuanqingContent.setText(data.sub_name);
                break;

            case 8: //失败
                layoutFail.setVisibility(View.VISIBLE);
                txtFailContent.setText(data.sub_name);
                txtFailMoney.setText(data.getBorrow_money_format().replace("￥", ""));
                txtFailReason.setText(data.failed_info);
                break;
        }
    }
}
